<template>
  <div class="goods-relevant">
    <div class="header">
      <i class="icon"></i>
      <span class="title">{{goodsId?'同类商品推荐':'猜你喜欢'}}</span>
    </div>
    <!-- 改造后的xtx-carousel.vue -->
    <xtxCarousel :sliders="sliders"></xtxCarousel>
  </div>
</template>

<script>
import xtxCarousel from '@/components/library/xtx-carousel.vue'
import { findRelevantGoods } from '@/api/product.js'
import { ref } from '@vue/reactivity'
export default {
  name: 'GoodsRelevant',
  props: {
    goodsId: {
      type: String,
      default: ''
    }
  },
  components: { xtxCarousel },
  setup (props) {
    // 最终需要的数据是sliders 提供给轮播图使用
    // 数据结构：sliders=[[4个商品],[4个商品],[4个商品],[4个商品]]
    const sliders = ref([])
    findRelevantGoods({ id: props.goodsId }).then(data => {
      // data.result 商品列表  数据结构 [16个商品]
      // console.log(data.result)
      // 将data.result数据按照新的数据结构赋值给sliders数据
      const pageSize = 4
      const pageCount = Math.ceil(data.result.length / pageSize)
      for (let i = 0; i < pageCount; i++) {
        sliders.value.push(data.result.slice(pageSize * i, pageSize * (i + 1)))
      }
      // console.log(sliders.value)
    })
    return { sliders }
  }
}
</script>

<style lang="less" scoped>
.goods-relevant{
  background: #fff;
  min-height: 460px;
  margin-top: 20px;
  .header{
    height: 80px;
    line-height: 80px;
    padding:0 20px;
    .title{
      font-size: 20px;
      padding-left: 10px;
    }
    .icon{
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before{
        content:"";
        width: 10px;
        height: 10px;
        position: absolute;
        left:0;
        top:2px;
        background: lighten(@xtxColor,40%);
      }
    }
  }
}
:deep(.xtx-carousel){
  height: 380px;
  .carousel{
    &-indicator{
      bottom: 30px;
      span {
        &.active{
          background: @xtxColor;
        }
      }
    }
    &-btn{
      top:110px;
      opacity: 1;
      background: rgba(0,0,0,0);
      color:#ddd;
      i{
        font-size: 30px;
      }
    }
  }
}
</style>
